/*
 * MaiaWorld.java
 *
 * Created on 26 maart 2008, 15:09
 */
package gui;

/**
 *
 * @author groep 11
 * @date 2007-2008
 */
import gui.panels.ArmyFunctions;
import gui.panels.VillageMap;
import gui.panels.FriendsControl;
import gui.panels.WorldMap;
import gui.panels.UpperBar;
import gui.panels.Chat;
import gui.panels.VillageInfo;
import gui.panels.BuildingFunctions;
import gui.panels.TradeFunctions;
import data.*;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import maiaclient.Maia;

public class MaiaWorld extends AbstrWorld {

    // Final fields
    final String MAIAWORLDTITLE = "Maia - ";
    final String IMAGEICONLOC = "src/img/logo.gif";
    // Fields
    private Maia maia;
    private Chat pnlChat;
    private Boolean ChooseWorld = false;
    private JPanel pnlRight;
    private JPanel pnlMaps;
    private VillageMap pnlVillageMap;
    private JPanel pnlWorldVillageFunctions;
    private BuildingFunctions pnlBuildingFunctions;
    private UpperBar balk;

    /** 
     * Creates new form MaiaWorld
     * @param maia 
     */
    public MaiaWorld(Maia maia) {
        try {

//+        
            setMaia(maia);
            maia.setMaiaWorld(this);

            maia.getMaiaClient().connect();
            maia.getMaiaClient().startAcceptMessageHandler();

            // Let server know you're in the world view.
            maia.getMaiaClient().signInToWorld();

            setUndecorated(true);
            setResizable(false);

            initComponents();
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice device = env.getDefaultScreenDevice();
            try {
                GraphicsConfiguration gc = device.getDefaultConfiguration();

                device.setFullScreenWindow(this);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            setTitle(MAIAWORLDTITLE + maia.getUser().getUsername());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //setDefaultLookAndFeelDecorated(true);
            setVisible(true);
            Image icon = Toolkit.getDefaultToolkit().getImage(IMAGEICONLOC);
            setIconImage(icon);
            JPanel inhoud = new JPanel();
            inhoud.setLayout(new BorderLayout());
            setContentPane(inhoud);


            balk = new UpperBar(this);
            inhoud.add(balk, BorderLayout.NORTH);

            // Panel at the right
            pnlRight = new JPanel();
            pnlRight.setPreferredSize(new Dimension(this.getSize().width / 4, 0));
            pnlRight.setBorder(new EmptyBorder(10, 10, 10, 10));
            pnlRight.setLayout(new GridLayout(2, 1));
            inhoud.add(pnlRight, BorderLayout.EAST);

            FriendsControl pnlVrienden = new FriendsControl(maia, this);
            pnlRight.add(pnlVrienden);

            VillageInfo pnlVillageInfo = new VillageInfo(maia);
            pnlRight.add(pnlVillageInfo);

            // Panel onderaan
            JPanel pnlOnderaan = new JPanel();
            pnlOnderaan.setPreferredSize(new Dimension(0, (this.getSize().height) / 5));
            pnlOnderaan.setLayout(new BorderLayout());

            inhoud.add(pnlOnderaan, BorderLayout.SOUTH);

            pnlBuildingFunctions = new BuildingFunctions(this);

            pnlChat = new Chat(maia, this);
            pnlChat.setPreferredSize(new Dimension(this.getSize().width / 3, 0));

            pnlWorldVillageFunctions = new JPanel();
            pnlWorldVillageFunctions.setLayout(new CardLayout());
            pnlWorldVillageFunctions.setPreferredSize(new Dimension((this.getSize().width * 2)/3, 0));

            JTabbedPane pnlTradeArmy = new JTabbedPane();

            pnlWorldVillageFunctions.add(pnlBuildingFunctions, "Village");
            pnlWorldVillageFunctions.add(pnlTradeArmy, "World");

            TradeFunctions pnlTradeFunctions = new TradeFunctions();
            ArmyFunctions pnlArmyFunctions = new ArmyFunctions();

            pnlTradeArmy.addTab("Army Functions", pnlArmyFunctions);
            pnlTradeArmy.addTab("Trade Functions", pnlTradeFunctions);

            pnlTradeArmy.setEnabledAt(1, false);

            pnlOnderaan.add(pnlWorldVillageFunctions, BorderLayout.CENTER);
            pnlOnderaan.add(pnlChat, BorderLayout.EAST);

            // Midden Panel met Cardlayout
            pnlMaps = new JPanel();
            pnlMaps.setLayout(new CardLayout());


            WorldMap pnlWorldMap = new WorldMap(this);
            pnlVillageMap = new VillageMap(this);
            pnlWorldMap.setPnlVillageInfo(pnlVillageInfo);

            pnlMaps.add(pnlVillageMap, "Village");
            pnlMaps.add(pnlWorldMap, "World");


            inhoud.add(pnlMaps, BorderLayout.CENTER);

            ChangeViewToVillage();
        } catch (IOException ex) {
            Logger.getLogger(MaiaWorld.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void ChangeViewToWorld() {

        pnlRight.setVisible(true);
        CardLayout clMaps = (CardLayout) getPnlMaps().getLayout();
        clMaps.show(getPnlMaps(), "World");

        CardLayout clFunctions = (CardLayout) pnlWorldVillageFunctions.getLayout();
        clFunctions.show(pnlWorldVillageFunctions, "World");
    }

    @Override
    public void ChangeViewToVillage() {

        pnlRight.setVisible(false);
        CardLayout cl = (CardLayout) getPnlMaps().getLayout();
        cl.show(getPnlMaps(), "Village");

        CardLayout clFunctions = (CardLayout) pnlWorldVillageFunctions.getLayout();
        clFunctions.show(pnlWorldVillageFunctions, "Village");

    }

    // <editor-fold defaultstate="collapsed" desc="Getters/Setters">  
    public void setMaia(Maia maia) {
        this.maia = maia;
    }

    public Maia getMaia() {
        return this.maia;
    }

    public Chat getChat() {

        return pnlChat;
    }

    public BuildingFunctions getPnlBuildingInfo() {
        return pnlBuildingFunctions;
    }

    @Override
    public Boolean getWorldView() {

        return ChooseWorld;
    }

    @Override
    public void setWorldView(Boolean newChooseWorld) {

        ChooseWorld = newChooseWorld;
    }

    @Override
    public JPanel getPnlMaps() {

        return pnlMaps;
    }

    public VillageMap getPnlVillageMap() {
        return pnlVillageMap;
    }
    public UpperBar getPnlUpperBar(){
        return balk;
    }

    // </editor-fold>  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
